<?php
namespace admin\service;

/**--tmpl-use--**/
use herosphp\core\Loader;

/**--tmpl-import--**/

class FriendLinkService extends CommonService{


    private $FriendlinkCateDao=null;
    private $adminDao=null;

    public function __construct($model) {
        parent::__construct($model);
        $this->FriendlinkCateDao = Loader::model('settingFriendlinkCate');
        $this->adminDao = Loader::model('AdminManager');
    }

    public function getItem($conditions, $fields, $order)
    {
        $item = parent::getItem($conditions, $fields, $order);
        if(!$item) return NULL;
        $friendlink_cate = $this->FriendlinkCateDao->getItem($item['catid']);
        $item['friendlink_cate_name'] = $friendlink_cate['name'];
        $item['friendlink_cate_description'] = $friendlink_cate['description'];
        $item['friendlink_cate_addtime'] = $friendlink_cate['addtime'];
        $item['friendlink_cate_parent_id'] = $friendlink_cate['parent_id'];
        $item['friendlink_cate_app_id'] = $friendlink_cate['app_id'];
        $item['friendlink_cate_manager_id'] = $friendlink_cate['manager_id'];
        $typemap = array (
            0 => '文字',
            1 => '图片',
        );
        $item['type_text'] = $typemap[$item['type']];
        return $item;
    }

    public function getItems($conditions, $fields, $order, $limit, $group, $having)
    {
        $items = parent::getItems($conditions, $fields, $order, $limit, $group, $having);
        if(!$items) return NULL;
        $friendlink_catecondi = array(0);
        $admin_condi = array();
        foreach($items as $k => $v)
        {
            if($v['catid']){
                $friendlink_catecondi[] = $v['catid'];
            }
            if($v['manager_id']){
                $admin_condi[] = $v['manager_id'];
            }
        }

        $friendlink_cates = $this->FriendlinkCateDao->getItems(array('id' => array( '$in' => $friendlink_catecondi)));
        $admins = $this->adminDao->getItems(array('id' => array( '$in' => $admin_condi)));
        $typemap = array (
            0 => '文字',
            1 => '图片',
        );
        foreach ($items as $k => $v)
        {
            foreach($admins as $admin)
            {
                if($v['manager_id']==$admin['id'])
                {
                    $items[$k]['manager_name'] = $admin['name'];
                }
            }
            foreach($friendlink_cates as $kk => $vv)
            {
                if($vv['id'] == $v['catid'])
                {
                    $items[$k]['friendlink_cate_name'] = $vv['name'];
                    $items[$k]['friendlink_cate_description'] = $vv['description'];
                    $items[$k]['friendlink_cate_addtime'] = $vv['addtime'];
                    $items[$k]['friendlink_cate_parent_id'] = $vv['parent_id'];
                    $items[$k]['friendlink_cate_app_id'] = $vv['app_id'];
                    $items[$k]['friendlink_cate_manager_id'] = $vv['manager_id'];

                }
            }
            $items[$k]['type_text'] = $typemap[$v['type']];

        }
        return $items;
    }
}

